package com.ssy.lingxi.system.service.impl;

import com.ssy.lingxi.common.constant.CommonConstant;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.PageModel;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.member.merchant.api.feign.paas.BaseLevelRightCreditControllerFeign;
import com.ssy.lingxi.member.merchant.api.model.vo.paas.BaseRightConfigStatusVO;
import com.ssy.lingxi.system.dao.MemberEquitiesRepository;
import com.ssy.lingxi.system.dto.request.MemberEquitiesUpdateRequest;
import com.ssy.lingxi.system.dto.response.MemberEquitiesResponse;
import com.ssy.lingxi.system.entity.MemberEquities;
import com.ssy.lingxi.system.service.MemberEquitiesService;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class MemberEquitiesServiceImpl implements MemberEquitiesService {
    @Resource
    MemberEquitiesRepository memberEquitiesRepository;
    @Resource
    private BaseLevelRightCreditControllerFeign baseLevelRightCreditControllerFeign;

    @Override
    public Wrapper<PageData<MemberEquitiesResponse>> memberEquitiesList(PageModel request) {
        Page<MemberEquities> all = memberEquitiesRepository.findAll(PageRequest.of(request.getCurrent(), request.getPageSize(), Sort.by("id").ascending()));
        List<MemberEquitiesResponse> list=new ArrayList<>();
        List<MemberEquities> memberEquities = all.toList();
        memberEquities.forEach(memberEquitie->{
            MemberEquitiesResponse memberEquitiesResponse=new MemberEquitiesResponse();
            BeanUtils.copyProperties(memberEquitie,memberEquitiesResponse);
            list.add(memberEquitiesResponse);
        });
        return Wrapper.success(new PageData(all.getTotalElements(),list));
    }

    @Override
    public Wrapper memberEquitiesUpdate(MemberEquitiesUpdateRequest request) {
        MemberEquities memberEquities = memberEquitiesRepository.findById(request.getId()).orElse(null);
        if(memberEquities == null){
            return Wrapper.fail("会员权益配置记录不存在");
        }

        //会员服务
        BaseRightConfigStatusVO statusVO = new BaseRightConfigStatusVO();
        statusVO.setTypeEnum(memberEquities.getTypeEnum());
        statusVO.setStatus(request.getType());
        Wrapper<?> result = baseLevelRightCreditControllerFeign.updateBaseRightStatus(statusVO);
        if(result.getCode() != ResponseCode.SUCCESS.getCode()) {
            return Wrapper.fail(result.getCode(), result.getMessage());
        }

        memberEquities.setState(request.getType()==1?1:0);
        memberEquitiesRepository.save(memberEquities);
        return Wrapper.success();
    }

    /**
     * 查询所有会员权益
     *
     * @author wrc
     * @date 2020/6/5
     */
    @Override
    public Wrapper<List<MemberEquities>> memberEquitiesAll() {
        List<MemberEquities> memberEquities = memberEquitiesRepository.queryByStateEquals(CommonConstant.State.STATE_START);
       return Wrapper.success(memberEquities);
    }
}
